package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.FromRequest;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.MD5;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.WebUpload;

public class Register extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		FromRequest newrequest = new FromRequest(getServletContext(), request,response);
		
		int sid =  TextUtil.String2int(newrequest.getParameterString("schoolid"),-1);
		String name = newrequest.getParameterString("name");
		String pwd = newrequest.getParameterString("pwd");
		String pwd2 = newrequest.getParameterString("pwd2");
		String email = newrequest.getParameterString("email");
		String sex = newrequest.getParameterString("sex");
		String age = newrequest.getParameterString("age");
		String pic = "userheads/default.png";
		int userno = 0;

		
		if (TextUtil.isEmpty(name)) {
			Repost.message("请填写用户名", request, response);
		} else if (TextUtil.isEmpty(pwd)) {
			Repost.message("请填写密码", request, response);
		} else if (TextUtil.isEmpty(pwd2)) {
			Repost.message("请填写确认密码", request, response);
		} else if (!pwd.equals(pwd2)) {
			Repost.message("两次输入的密码不一致，请重新填写", request, response);
		} else if (TextUtil.isEmpty(email)) {
			Repost.message("请填写邮箱", request, response);
		} else if (!TextUtil.isEmail(email)) {
			Repost.message("请填写正确的邮箱地址", request, response);
		} else {
			Connection conn = DBUtil.getConnection();
			if (conn != null) {
				DAOFactory dao = DAOFactory.init(conn, request, response);
				UserDao udao = dao.getUserDao();
				DynamicDao ddao = dao.getDynamicDao();
				
				if (!udao.isRegister(sid,name)) {
					//MD5（userName+MD5（pwd））；
					pwd = MD5.getMD5(name + MD5.getMD5(pwd));//app提交的md5
					pwd = MD5.getMD5(pwd);//存入数据库的md5
					
					userno = udao.getRegisterId();
					
					UserInfo info = new UserInfo();
					info.setSchoolid(sid);
					info.setUsername(name);
					info.setPassword(pwd);
					info.setEmail(email);
					info.setSex(TextUtil.String2int(sex, 0));
					info.setAge(TextUtil.String2int(age, 0));
					info.setUserhead(pic);
					
					//保存图片
					FileItem fileitem = (FileItem) newrequest.getParameter("pic");
					if (fileitem != null) {
						pic = userno + "."+ WebUpload.getExtensionName(fileitem.getName());
						if (WebUpload.save(getServletContext(), fileitem,"/userheads/", pic)) {
							info.setUserhead("userheads/" + pic);
						}
					}
					
					if (udao.register(info)){
						info = udao.verification(sid,name, pwd);
						if (ddao.addSystemDynamic(Constant.DYNAMICSYS_regeist, info.getId(),-1, "注册了最爱学!", Constant.PIC_register)) {
							//添加记录成功！
						}
						Repost.message("注册成功！", request, response);
					} else {
						Repost.message("注册失败！", request, response);
					}
				} else {
					Repost.message("该账号已被注册！", request, response);
				}
			} else {
				Repost.dberror(request, response);
			}
		}
	}

}
